package com.haha;

import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

import static com.haha.ByteBufferUtil.debugAll;

public class TestByteBufferString {
    public static void main(String[] args) {
        //1.字符串转为ByteBuffer
        ByteBuffer buffer = ByteBuffer.allocate(16);
        byte[] bytes = "hello".getBytes(StandardCharsets.UTF_8);
        buffer.put(bytes);
        debugAll(buffer);
        //2.charset
        ByteBuffer buffer1 = StandardCharsets.UTF_8.encode("hello");
        debugAll(buffer1);
        //3.wrap
        ByteBuffer buffer2 = ByteBuffer.wrap("hello".getBytes(StandardCharsets.UTF_8));
        debugAll(buffer2);
        System.out.println(StandardCharsets.UTF_8.decode(buffer1).toString());
        System.out.println(StandardCharsets.UTF_8.decode(buffer2).toString());
        buffer.flip();
        System.out.println(StandardCharsets.UTF_8.decode(buffer).toString());
    }
}
